﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Windows.Forms;

namespace MetaDemo.Forms
{
    public partial class InfoButtonForm : Form
    {
        PictureButton controlBtn = null;

        PictureButton codeBtn = null;

        PictureButton loadAssemblyBtn = null;
        
        PictureButton closeBtn = null;

        Control controlUnderMouse = null;
        Control ControlUnderMouse { get { return controlUnderMouse; } }

        public InfoButtonForm()
        {
            InitializeComponent();

            FormBorderStyle = FormBorderStyle.None;
            BackColor = Color.Gray;
            TransparencyKey = BackColor;

            InitializePictureButton(ref controlBtn, "control", 0, this.ShowControlInfo);
            InitializePictureButton(ref codeBtn, "code", PictureButton.BUTTON_WIDTH + PictureButton.BUTTON_PADDING, this.ShowCode);
            InitializePictureButton(ref loadAssemblyBtn, "assembly", 2 * (PictureButton.BUTTON_WIDTH + PictureButton.BUTTON_PADDING), this.LoadAssembly);
            InitializePictureButton(ref closeBtn, "close", 3 * (PictureButton.BUTTON_WIDTH + PictureButton.BUTTON_PADDING), this.HideButtons);
        }

        private void InitializePictureButton(ref PictureButton picButton, string name, int X, EventHandler eHandler)
        {
            picButton = new PictureButton(name);
            picButton.Parent = this;
            picButton.Location = new Point(X, 0);
            picButton.Click += new EventHandler(eHandler);
        }

        public void Show(Control c)
        {
            if (null == UIHelper.GetFormForControl(c))
                return;

            controlUnderMouse = c;

            AdjustPosition(c);
            Show();
        }

        public void HideButtons(Object sender, EventArgs args)
        {
            Hide();
        }

        public void LoadAssembly(Object sender, EventArgs args)
        {
            Forms.LoadAssemblyForm.Get().Show();
        }

        public void ShowControlInfo(Object sender, EventArgs args)
        {
            Object o = ControlUnderMouse;

            if (o is Control)
                Forms.ObjectInfoForm.Get((Control)o).Show();
            else if (o is Assembly)
                Forms.ObjectInfoForm.Get((Assembly)o).Show();
            else
                Forms.ObjectInfoForm.Get(o).Show();
        }

        public void ShowCode(Object sender, EventArgs args)
        {
            Forms.CompileCodeForm.Get((Object)ControlUnderMouse).Show();
        }

        public void Locate(Object sender, EventArgs args)
        {
            AdjustPosition(ControlUnderMouse);
        }

        public void AdjustPosition(Control c)
        {
            Form form4Control = UIHelper.GetFormForControl(c);
            if (form4Control == null)
            {
                return;
            }

            Point p_self = new Point(), p_form = form4Control.Location;

            p_self.X = p_form.X;
            if (Width < form4Control.Width)
            {
                p_self.X += (form4Control.Width - Width) / 2;
            }

            p_self.Y = p_form.Y - PictureButton.BUTTON_HEIGHT - PictureButton.BUTTON_PADDING;
            Location = p_self;
        }

        private void Form1_Load(object sender, EventArgs e)
        {}

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // InfoButtonForm
            // 
            this.ClientSize = new System.Drawing.Size(158, 32);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "InfoButtonForm";
            this.ShowInTaskbar = false;
            this.ResumeLayout(false);

        }

    }
}
